<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accountlog extends CI_Controller {
	
	private $tab ='account';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	function accountloglist(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$ftype 		= $this->input->get('ftype');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		$this->load->model('Accountlog_model');
		
		$data = array();
		$user_id = 0;
		if($username != ''){
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			$data['where']['user_id'] = $user_id;
		}else{
			$vdata['user_loan'] 			= $this->Accountlog_model->arrays("var_user_loan");
			$vdata['account_type'] 			= $this->Accountlog_model->arrays("account_type");
			$vdata['username'] 	= $username;
			$vdata['ftype'] 	= $ftype;
			$vdata['strtime'] 	= $strtime;
			$vdata['endtime'] 	= $endtime;
			
			$vdata['tab'] = $this->tab;
		    $vdata['sub'] = 'often';
		
			$this->load->view('/ychadmin/accountlog_list', $vdata);
			return;
		}
		if($ftype != '' && $ftype != 0){
			$data['where']['ftype'] = $ftype;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$vdata = $this->Accountlog_model->get_accountlog_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/accountlog/accountloglist/?username='.$username.'&ftype='.$ftype.'&strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['accountlog_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['user_loan'] 			= $this->Accountlog_model->arrays("var_user_loan");
		$vdata['account_type'] 			= $this->Accountlog_model->arrays("account_type");
		$vdata['username'] 	= $username;
		$vdata['ftype'] 	= $ftype;
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'often';
		
		$this->load->view('/ychadmin/accountlog_list', $vdata);
	}
	

	function accountlogalllist(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$ftype 		= $this->input->get('ftype');
		$utype 		= $this->input->get('utype');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		$user_id = 0;
		if($username != ''){
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			$data['where']['user_id'] = $user_id;
		}
		if($ftype != '' && $ftype != 0){
			$data['where']['ftype'] = $ftype;
		}
		if($utype != '' && $utype != -1){
			$data['where']['utype'] = $utype;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Accountlog_model');
		$vdata = $this->Accountlog_model->get_accountlogall_list($data);
		
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/accountlog/accountlogalllist/?username='.$username.'&ftype='.$ftype.'&utype='.$utype.'&strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['accountlog_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		$vdata['user_loan'] 			= $this->Accountlog_model->arrays("var_user_loan");
		$vdata['account_type'] 			= $this->Accountlog_model->arrays("account_type");
		$vdata['username'] 	= $username;
		$vdata['ftype'] 	= $ftype;
		$vdata['utype'] 	= $utype;
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'accountlog';
		
		$this->load->view('/ychadmin/accountlogall_list', $vdata);
	}
	
	function accountlogexcel(){
		$username 	= $this->input->get('username');
		$ftype 		= $this->input->get('ftype');
		$fstatus 	= $this->input->get('fstatus');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		$user_id = 0;
		if($username != ''){
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			$data['where']['user_id'] = $user_id;
		}
		if($ftype != '' && $ftype != 0){
			$data['where']['ftype'] = $ftype;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['limit'] = 'all';
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = 0;
		$this->load->model('Accountlog_model');
		$vdata = $this->Accountlog_model->get_accountlog_list($data);
		$user_loan 				= $this->Accountlog_model->arrays("var_user_loan");
		$account_type 			= $this->Accountlog_model->arrays("account_type");
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
		
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("编号", "用户名", "真实姓名", "用户类型", "类型", "账户总额", "操作金额", "可用金额", "冻结金额", "代收金额", "交易方", 
						"备注", "操作时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$accountlog_list = $vdata['accountlog_list'];
		for($i = 0; $i < count($accountlog_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $accountlog_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $accountlog_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $accountlog_list[$i]['realname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $user_loan[$accountlog_list[$i]['is_loan']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $account_type[$accountlog_list[$i]['ftype']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $accountlog_list[$i]['total']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $accountlog_list[$i]['money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $accountlog_list[$i]['use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $accountlog_list[$i]['no_use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $accountlog_list[$i]['collection']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, $accountlog_list[$i]['to_user']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $accountlog_list[$i]['remark']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i+2, date('Y-m-d H:i:s', $accountlog_list[$i]['add_time']));
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="资金日志.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}	
	
	function accountlogallexcel(){
		$username 	= $this->input->get('username');
		$ftype 		= $this->input->get('ftype');
		$utype 		= $this->input->get('utype');
		$fstatus 	= $this->input->get('fstatus');
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		$user_id = 0;
		if($username != ''){
			$this->load->model('User_model');
			$user_id = $this->User_model->get_userid_by_username($username);
			$data['where']['user_id'] = $user_id;
		}
		if($ftype != '' && $ftype != 0){
			$data['where']['ftype'] = $ftype;
		}
		if($utype != '' && $utype != -1){
			$data['where']['utype'] = $utype;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['limit'] = 'all';
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = 0;
		$this->load->model('Accountlog_model');
		$vdata = $this->Accountlog_model->get_accountlogall_list($data);
		$user_loan 			= $this->Accountlog_model->arrays("var_user_loan");
		$account_type 			= $this->Accountlog_model->arrays("account_type");
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
		
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("编号", "用户名", "真实姓名", "用户类型", "类型", "账户总额", "操作金额", "可用金额", "冻结金额", "代收金额", "交易方", 
						"备注", "操作时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$accountlog_list = $vdata['accountlog_list'];
		for($i = 0; $i < count($accountlog_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $accountlog_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $accountlog_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $accountlog_list[$i]['realname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $user_loan[$accountlog_list[$i]['is_loan']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $account_type[$accountlog_list[$i]['ftype']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $accountlog_list[$i]['total']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $accountlog_list[$i]['money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $accountlog_list[$i]['use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $accountlog_list[$i]['no_use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $accountlog_list[$i]['collection']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, $accountlog_list[$i]['to_user']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $accountlog_list[$i]['remark']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i+2, date('Y-m-d H:i:s', $accountlog_list[$i]['add_time']));
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="资金日志.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}	
}